package com.hansen.library.ui.widget.nav;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.design.widget.TabLayout;
import android.util.AttributeSet;

import com.hansen.library.R;
import com.hansen.library.utils.ColorUtils;

/**
 * @author han
 * @date 2019/9/19
 */
public class BorderTabLayout extends TabLayout {

    //需要左边界
    private boolean hasLeftBorder;
    //需要上边界
    private boolean hasTopBorder;
    //需要右边界
    private boolean hasRightBorder;
    //需要下边界
    private boolean hasBottomBorder;
    //中间
    private boolean hasCenterBorder;
    //border 上下间距
    private int mBorderPadding;

    private Paint mPaint;
    private Path mPath;

    private int mBorderColor;
    private int mBorderWidth;

    public BorderTabLayout(Context context) {
        this(context, null);
    }

    public BorderTabLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BorderTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        if(attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BorderLayoutStyle, defStyleAttr, 0);

            hasLeftBorder = a.getBoolean(R.styleable.BorderLayoutStyle_my_border_left, false);
            hasTopBorder = a.getBoolean(R.styleable.BorderLayoutStyle_my_border_top, false);
            hasRightBorder = a.getBoolean(R.styleable.BorderLayoutStyle_my_border_right, false);
            hasBottomBorder = a.getBoolean(R.styleable.BorderLayoutStyle_my_border_bottom, false);
            hasCenterBorder = a.getBoolean(R.styleable.BorderLayoutStyle_my_border_center, false);

            mBorderWidth = a.getDimensionPixelSize(R.styleable.BorderLayoutStyle_my_border_width, 1);
            mBorderPadding = a.getDimensionPixelOffset(R.styleable.BorderLayoutStyle_my_border_padding, 0);

            mBorderColor = a.getColor(R.styleable.BorderLayoutStyle_my_border_color, ColorUtils.getColorById(context, R.color.color_d8d8d8));

            a.recycle();

        }

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mBorderColor);
        mPaint.setStrokeWidth(mBorderWidth);
        mPaint.setAntiAlias(true);

        mPath = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (hasLeftBorder) {
            mPath.reset();
            mPath.moveTo((float) (0 + mBorderWidth / 2.0), 0 + mBorderPadding);
            mPath.lineTo((float) (0 + mBorderWidth / 2.0), this.getHeight() - mBorderPadding);
            canvas.drawPath(mPath, mPaint);
        }

        if (hasTopBorder) {
            mPath.reset();
            mPath.moveTo(0 + mBorderPadding, (float) (0 + mBorderWidth / 2.0));
            mPath.lineTo(this.getWidth() - mBorderPadding, (float) (0 + mBorderWidth / 2.0));
            canvas.drawPath(mPath, mPaint);
        }

        if (hasRightBorder) {
            mPath.reset();
            mPath.moveTo((float) (this.getWidth() - mBorderWidth / 2.0), 0 + mBorderPadding);
            mPath.lineTo((float) (this.getWidth() - mBorderWidth / 2.0), this.getHeight() - mBorderPadding);
            canvas.drawPath(mPath, mPaint);
        }

        if (hasBottomBorder) {
            mPath.reset();
            mPath.moveTo(0 + mBorderPadding, (float) (this.getHeight() - mBorderWidth / 2.0));
            mPath.lineTo(this.getWidth() - mBorderPadding, (float) (this.getHeight() - mBorderWidth / 2.0));
            canvas.drawPath(mPath, mPaint);
        }
        if (hasCenterBorder) {
            mPath.reset();
            mPath.moveTo(0, this.getHeight() / 2);
            mPath.lineTo(this.getWidth(), this.getHeight() / 2);

            canvas.drawPath(mPath, mPaint);
        }
    }
}
